Literals are sequences of characters that are a literal representation of instances of one of a special set of classes. The value of a literal is the object it represents. Literals are tokens in the input stream that the bytecode compiler recognizes as objects. ScriptX provides string literals, name literals, numeric constants, and several kinds of collections and ranges as literal objects.
symbol ::= initialChar [ trailingChar ]*
initialChar ::= alphaChar | underscore
trailingChar ::= alphaChar | underscore | decimalDigit
alphaChar ::= a | b | c | . . . | x | y | z
decimalDigit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
underscore ::= _
A symbol is a lexical name, a name that is understood by the compiler. The compiler associates a symbol at compile time with the thing in a program that it names, such as a variable, constant, or function.
RootObject
class provides each object with a default version of the methods isComparable
, localLT
, localEqual
, and eq
. These four generic functions are visible to the scripter. A class can override these generics to provide its own version of the Comparison protocol. In this way, the number and string classes have different definitions of equality. Table A-2 indicates the precedence and associativity of operators in the ScriptX language. In this table, precedence is ordered from highest to lowest.
Most ScriptX operators can be used with or without white space. Careful use of white space, which is defined on page 233, can make a program more readable. Certain operators require a separator to prevent ambiguity. Operators that are reserved words, such as contains
or as
, must be set off by a separator, normally a blank. The subtraction operator requires a separator, normally a blank, so that the compiler can distinguish it from the negation operator.
In this grammar, those punctuation marks that have a visual printed representation, such as the comma or colon, are depicted in literal form, in the same typeface and style as reserved words. Nonprinting punctuation marks, such as the end-of-line character, are indicated in certain cases.
White space is a concept that goes hand-in-hand with punctuation. Any input characters that the compiler ignores are called white space. ScriptX has two kinds of white space. First, there are comments. ScriptX identifies a sequence of characters that begins with two hyphens ( --
) or two slashes ( //
) and ends with a new line or carriage return as a comment. A comment can be inserted in the middle of an expression; interpretation of the expression will resume on the next line. ScriptX also accepts C-style inset comments, using the same delimiters. ScriptX, in contrast with ANSI C, allows inset comments to be nested.
A second kind of white space is blank space. Blank space consists of optional space, tab, and end-of-line characters that can be inserted between tokens to make code more readable. Certain punctuation marks can serve as white space, while others cannot. If a punctuation mark can serve as white space, then two or more are permitted wherever one is permitted. Table A-4 lists punctuation marks in the ScriptX language, identifying whether or not they can be used as white space.
In most cases, this grammar does not explicitly indicate where end-of-line and white space characters are allowed, except where an end-of-line character is required as a separator. An incomplete sentence can be broken with a newline character; evaluation of the expression resumes on the following line. ScriptX programmers commonly break lines after a binary operator, after a separator that cannot act as white space, or anywhere where the compiler is expecting a closing delimiter such as a parenthesis.
Parentheses serve as separators as well as delimiters in expressions that could otherwise not be parsed. The following examples demonstrate how parentheses can be used to turn an expression into a factor. Factors, indivisible syntactic units, are discussed in the section "Types of Expression" on page 236.
function doIt a b -> (
print a; print b
)
doIit -1 -2
-1
-2
doIt -(1) -(2)
no sub instance method
doIt negate(1) negate(2)
too many arguments 4 supplied 2 allowed
doIt (negate(1)) (negate(2))
-1
-2
As these examples demonstrate, ScriptX allows lists of arguments with minimal punctuation. If arguments are not separated by commas, then the arguments themselves must be factors. By enclosing an expression that is used as an argument within parentheses, it becomes a factor, a complete and indivisible unit of punctuation.
ScriptX punctuation allows for a variety of programming styles; its flexibility accounts for the fluidity of ScriptX code. Punctuation usually causes few difficulties, even for beginning scripters. For more information on punctuation, see the discussion that begins on page 29 of this volume.
NameClass
. Any valid name that is preceded by an at sign ( @
) is interned. Name literals are full-fledged NameClass
objects that have the same value at compile time and run time. They are used as labels in programs, often to represent a state or outcome. Two name literals that have the same value are the same object. That is, the names @insideOut
, @insideout
, and @INSIDEOUT
are not merely equal, they are actually the same object.nameLiteral ::= @ [ trailingChar ]+
trailingChar ::= alphaChar | underscore | decimalDigit
A string literal is a sequence of Unicode characters, enclosed in double quotes in the input stream. A string literal can extend over multiple lines, can be any length, and can include any valid Unicode character, including newline. To use certain nonprinting characters in a string, escape characters are required.
stringLiteral ::= " [ unicodeChar ]* "
unicodeChar ::= -- any printing char
| escapeChar
| \< hexConst >
escapeChar ::= \n | \r | \t
hexDigit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | a | b | c | d | e | f
hexConst ::= [ hexDigit ]+
The compiler automatically stores a string literal as a StringConstant
object. To modify or edit a string literal, it must first be converted to the String
or Text
class. Although name literals and symbols are not case sensitive in ScriptX, strings are. For more information on strings, see the "Text and Fonts" chapter of ScriptX Components Guide.
A numeric constant is automatically stored as an instance of one of the subclasses of Number
: ImmediateInteger
, LargeInteger
, ImmediateFloat
, or Float
, depending on range and precision requirements. An integer constant is stored as an ImmediateInteger
object, except when its value extends beyond the 29-bit storage range of the class. ScriptX has no unsigned data types. A floating point constant is converted to either an ImmediateFloat
or a Float
object depending on both range and precision requirements. For more information, see the "Numerics" chapter of ScriptX Components Guide.
numericConst ::= mantissa [ exponent ]
| hexLiteral
mantissa ::= integerConst [ .decimalDigit ] [ decimalDigit ]*
exponent ::= e integerConst
integerConst ::= [ negOperator ] [ decimalDigit ]+
negOperator ::= -
decimalDigit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
hexLiteral ::= 0x [ hexDigit ]+
The ScriptX language provides several other literal constructions. A literal construction is an expression that creates a new instance of a class directly. Normally, you create a new class either by calling the new
method or by using the object
expression. ScriptX creates instances of Array
, KeyedLinkedList
, ContinuousNumberRange
, and NumberRange
from literal expressions.
arrayLiteral ::= #( exprList )
| #( keyedList )
| #( )
| #( : )
exprList ::= simpleExpr [ , exprList ]*
keyedList ::= factor : simpleExpr [ , keyedList ]*
rangeLiteral ::= factor to factor [ by factor ]
| factor by factor [ by factor ]
| factor to factor [ inclOption ] continuous
| factor inclOption to factor [ inclOption ] continuous
inclOption ::= inclusive | exclusive
These collections and ranges can also be instantiated by normal means, by calling new
on the appropriate class or by using an object definition expression.
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.